﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCCN.BLL;
using SCCN.DAL;
using SCCN.Utility;

public partial class Admin_FlinkList : System.Web.UI.Page
{
    FriendLinksBLL bllFriendLinks = FriendLinksBLL.getInstance();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRpt();
        }
    }

    void BindRpt()
    {
        SC_FriendLinks obj = new SC_FriendLinks();
        obj.FriendLink = this.txtFriendLinkName.Text;
        obj.FriendLinkUrl = string.Empty;
        obj.IsDelete = false;
        int rowCount = 0;
        this.rptList.DataSource = bllFriendLinks.GetFriendLinksByCondition(obj, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out rowCount);
        this.rptList.DataBind();
        this.AspNetPager1.RecordCount = rowCount;
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindRpt();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        this.txtFriendLink.Text = string.Empty;
        this.txtFriendLinkUrl.Text = string.Empty;
        this.txtFriendLinkName.Text = string.Empty;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.fup.HasFile)
        {
            string FileName = fup.PostedFile.FileName;
            string upload_file = Server.MapPath("~/") + "upload/friendlinks/" + FileName;
            fup.PostedFile.SaveAs(upload_file);//开始上传文件
            this.lblImgUrl.Text = "/upload/friendlinks/" + FileName; ;
        }
        int id = 0;
        if (this.Hid.Value.Length == 0)
        {
            SC_FriendLinks obj = new SC_FriendLinks();
            obj.FriendLink = this.txtFriendLink.Text;
            obj.FriendLinkUrl = this.txtFriendLinkUrl.Text;
            obj.OrderId = Convert.ToInt32(this.txtOrderId.Text);
            obj.FLImgUrl = this.lblImgUrl.Text;
            obj.IsDelete = false;
            if (bllFriendLinks.Add(obj))
            {
                HttpHelper.Alert("保存成功！", Page);
                BindRpt();
                this.txtFriendLink.Text = string.Empty;
                this.txtFriendLinkName.Text = string.Empty;
                this.txtFriendLinkUrl.Text = string.Empty;
                this.txtOrderId.Text = string.Empty;
            }
            else
            {
                HttpHelper.Alert("保存失败！", Page);
            }
        }
        else
        {
            int.TryParse(this.Hid.Value, out id);
            SC_FriendLinks obj = bllFriendLinks.GetFriendLinksById(id);
            obj.FriendLink = this.txtFriendLink.Text;
            obj.FriendLinkUrl = this.txtFriendLinkUrl.Text;
            obj.OrderId = Convert.ToInt32(this.txtOrderId.Text);
            obj.FLImgUrl = this.lblImgUrl.Text;
            obj.IsDelete = false;
            if (bllFriendLinks.Update(obj))
            {
                HttpHelper.Alert("保存成功！", Page);
                BindRpt();
                this.txtFriendLink.Text = string.Empty;
                this.txtFriendLinkName.Text = string.Empty;
                this.txtFriendLinkUrl.Text = string.Empty;
                this.txtOrderId.Text = string.Empty;
            }
            else
            {
                HttpHelper.Alert("保存失败！", Page);
            }
        }
    }

    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = 0;
        id = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "Del")
        {
            if (bllFriendLinks.Delete(id))
            {
                HttpHelper.Alert("删除成功！", Page);
                BindRpt();
            }
        }
        if (e.CommandName == "Update")
        {
            SC_FriendLinks obj = bllFriendLinks.GetFriendLinksById(id);
            this.txtFriendLink.Text = obj.FriendLink;
            this.txtFriendLinkUrl.Text = obj.FriendLinkUrl;
            this.txtOrderId.Text = obj.OrderId.ToString();
            this.Hid.Value = obj.Id.ToString();
            this.lblImgUrl.Text = obj.FLImgUrl;
        }
    }
}